package umage.core;

import javax.swing.JDialog;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Rectangle;

import javax.swing.AbstractAction;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import umage.projects.ProjectInfo;
import umage.projects.UMageProject;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ListSelectionModel;

public class NewDialog extends JDialog {
	protected JButton submitButton;
	@SuppressWarnings("rawtypes")
	protected JList projectList;
	protected boolean projectConfirmed;
	
	protected class ProjectCellRenderer extends DefaultListCellRenderer {
		
		public ProjectCellRenderer() {}
		
		@Override
		public Component getListCellRendererComponent(JList<?> list,
				Object value, int index, boolean isSelected,
				boolean cellHasFocus) {
			JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
			if (value instanceof Class) {
				// Code for naming and imaging the projects in the list
				
				Class<?> classValue = (Class<?>)value;
				ProjectInfo projectInfo = classValue.getAnnotation(ProjectInfo.class);
				String classLabel = null;
				Icon classIcon = null;
				if (projectInfo != null) {
					classLabel = projectInfo.name();
					classIcon = ImageUtil.getImageIcon(projectInfo.icon(), "/umage/projects/images/");
				}
				if (classLabel == null) classLabel = classValue.getSimpleName();
				label.setText(classLabel);
				if (classIcon != null) label.setIcon(classIcon);
			}
			return label;
		}
		
	}

	/**
	 * Create the dialog.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" }) // Used to deal with WindowBuilder generated code
	public NewDialog(Class<UMageProject>[] projectTypes) {
		projectConfirmed = false;
		
		setTitle("New Project");
		setModalityType(ModalityType.APPLICATION_MODAL);
		setBounds(100, 100, 450, 300);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		submitButton = new JButton("Submit");
		submitButton.setEnabled(false);
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doSubmit();
			}
		});
		panel.add(submitButton);
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doCancel();
			}
		});
		panel.add(cancelButton);
		
		JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		// To prevent testing crashes
		if (projectTypes == null) projectTypes = (Class<UMageProject>[])new Class[0];
		
		projectList = new JList(projectTypes);
		projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		projectList.setCellRenderer(new ProjectCellRenderer());
		projectList.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				submitButton.setEnabled(projectList.getSelectedValue() != null);
			}
		});
		projectList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() >= 2) {
					Rectangle bounds = projectList.getCellBounds(0, projectList.getLastVisibleIndex());
					if (bounds != null && bounds.contains(e.getPoint())) {
						doSubmit();
					}
				}
			}
		});
		
		// Set up submitting with the enter key
		KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
		projectList.getActionMap().put(enterKeyStroke, new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				doSubmit();
			}
		});
		projectList.getInputMap(JList.WHEN_FOCUSED).put(enterKeyStroke, enterKeyStroke);
		projectList.getInputMap(JList.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, enterKeyStroke);
		
		scrollPane.setViewportView(projectList);
		
	}

	protected void doSubmit() {
		if (projectList.getSelectedValue() != null) {
			projectConfirmed = true;
			setVisible(false);
		}
	}
	
	protected void doCancel() {
		setVisible(false);
	}
	
	public boolean isProjectConfirmed() {
		return projectConfirmed;
	}
	
	public void clearProjectConfirmed() {
		projectConfirmed = false;
	}
	
	@SuppressWarnings("unchecked")
	public Class<UMageProject> getSelectedProject() {
		return (Class<UMageProject>) projectList.getSelectedValue();
	}
	
}
